<?php
/**
* product.php
*
* Create on 2014-12-26
* Create by TT
*/
namespace haibao\cms\business;

use \haibao\cms\data\Product as ProductData;

class Product extends \haibao\cms\business\Base{
	
	/**
	 * 通过单品ID得到单品实体
	 * @param unknown_type $productId
	 */
	public static function getProductInfo($productId){
		if(empty($productId)){
			throw new BusinessException('单品ID不能为空',self::DATA_ERROR_PRODUCT_ID);
		}
		$productData = new ProductData();
		return $productData->getOneById($productId);
	}
	
	/**
	 * 通过单品Id取单品信息
	 * @param string $idStr
	 */
	public static function getProductInfoByIds($idStr){
		$productData = new ProductData();
		return $productData->getProductInfoByIds($idStr);
	}
	
	/**
	 * 
	 */
	public static function getProductValueByIds($productIdStr,$isSign = false){
	    $productData = new ProductData();
	    return $productData->getProductValueByIds($productIdStr,$isSign);
	}
	
	/**
	 * 添加单品
	 * @param \haibao\cms\model\data\Product $model	单品实体
	 * @param array $brandIds	所属品牌ID
	 */
	public static function addProduct($model,$brandIds){
		self::checkOperation(self::FUNC_CODE_PRODUCT_ADD);
		
		if(empty($model)){
			throw new BusinessException('单品不能为空',self::DATA_EMPTY);
		}
		if(empty($brandIds)){
			throw new BusinessException('单品所属品牌不能为空',self::DATA_EMPTY_ID);
		}
		/* if(empty($model->ProductName)){
			throw new BusinessException('单品名称不能为空',self::DATA_ERROR_PRODUCT_NAME);
		} */
		if(empty($model->CategoryId)){
			throw new BusinessException('单品所属分类不能为空',self::DATA_ERROR_PRODUCT_CATEGORY_ID);
		}
		$model->CreateBy = self::getCurrentUser()->Id;
		$model->CreateTime = new \DateTime();
		$productData = new ProductData();
		$productData->addProduct($model, $brandIds,self::getCurrentUser()->UserName);
		
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_ADD, $model->toArray(true,true));
	}
	
	/**
	 * 修改产品
	 * @param int $id	单品ID
	 * @param array $arrSet 需要修改的字段
	 * @param array $brandIds	所属品牌ID
	 * @param int $categoryChange	是否修改类别
	 */
	public static function updateProduct($id, $arrSet,$brandIds,$categoryChange = 0){
		self::checkOperation(self::FUNC_CODE_PRODUCT_ADD);
		if(empty($id)){
			throw new BusinessException('单品ID不能为空',self::DATA_ERROR_PRODUCT_ID);
		}
		if(empty($brandIds)){
			throw new BusinessException('单品所属品牌不能为空',self::DATA_EMPTY_ID);
		}
		if(empty($arrSet)){
			throw new BusinessException('需要修改的数据不能为空',self::DATA_EMPTY);
		}
		$productData = new ProductData();
		$productData->updateProduct($id, $arrSet,$brandIds,$categoryChange,self::getCurrentUser()->UserName);
		$arrSet['Id'] = $id;
		$arrSet['BrandIds'] = $brandIds;
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_UPDATE, $arrSet);
	}
	
	public static function updateProductStatus($id,$status){
		self::checkOperation(self::FUNC_CODE_PRODUCT_ADD);
		if(empty($id)){
			throw new BusinessException('单品ID不能为空',self::DATA_ERROR_PRODUCT_ID);
		}
		$productData = new ProductData();
		$productData->updateProductStatus($id, $status);
		$arrSet['Id'] = $id;
		$arrSet['Status'] = $status;
		$arrSet['CreateBy'] = self::getCurrentUser ()->Id;
		$arrSet['UpdateTime'] = new \DateTime();
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_UPDATE, $arrSet);
	}
	
	/**
	 * 查询产品
	 * @param \haibao\cms\data\filter\Product $productFilter
	 */
	public static function getProduct($productFilter){
		$productData = new ProductData();
		return $productData->getAll($productFilter);
	}
	
	/**
	 * 查询单个产品
	 * @param \haibao\cms\data\filter\Product $productFilter
	 */
	public static function getOneProduct($productFilter){
		$productData = new ProductData();
		return $productData->getOne($productFilter);
	}
	
	/**
	 * 通过单品ID得到对应的品牌ID
	 * @param int $productId
	 */
	public static function getBrandIdByProductId($productId){
		$productData = new ProductData();
		return $productData->getBrandIdByProductId($productId);
	}
	
	/**
	 * 获得老单品
	 * @param unknown $productIds
	 * @param string $isNewId
	 * @return Ambigous <multitype:, multitype:Ambigous <NULL, \DateTime> >
	 */
	public static function getOldProductId($productIds,$isNewId = true){
		$productData = new ProductData();
		return $productData->getOldproductId($productIds,$isNewId);
	}
}